import { ICompatibleAdapter, AdapterInfo, ProtocolType } from '../core/interfaces';
import { execSync } from 'child_process';

export class NativeRegistryAdapter implements ICompatibleAdapter {
    getInfo(): AdapterInfo {
        return {
            name: 'NativeRegistryAdapter',
            version: '1.0.0',
            target: 'win32',
            protocol: ProtocolType.REGISTRY,
            description: 'Direct registry access via reg.exe'
        };
    }

    isSupported(): boolean {
        return process.platform === 'win32';
    }

    supportsProtocol(protocol: ProtocolType): boolean {
        return protocol === ProtocolType.REGISTRY;
    }

    async execute<T extends string>(command: T): Promise<T> {
        return this.rawRegistryCommand(command) as unknown as Promise<T>;
    }

    private rawRegistryCommand(command: string): string {
        try {
            const result = execSync(`reg ${command}`, { 
                stdio: 'pipe',
                windowsHide: true
            });
            return result.toString();
        } catch (error) {
            const message = error instanceof Error ? error.message : 'Unknown error';
            throw new Error(`Registry command failed: ${message}`);
        }
    }

    // 封装常用注册表操作
    async queryValue(key: string, value: string): Promise<string> {
        return this.rawRegistryCommand(`query "${key}" /v "${value}"`);
    }

    async addKey(key: string): Promise<boolean> {
        this.rawRegistryCommand(`add "${key}" /f`);
        return true;
    }
}